/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.basic.hazard.source.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.basic.hazard.source.dto.HazHazardSourceDTO;
import com.hopes.bizz.basic.hazard.source.dto.HazHazardSourceExcelDTO;
import com.hopes.bizz.basic.hazard.source.dto.HazHazardSourceVideoDTO;
import com.hopes.bizz.basic.hazard.source.entity.HazHazardSourceEntity;
import com.hopes.bizz.basic.hazard.source.service.HazHazardSourceService;
import com.hopes.bizz.basic.hazard.source.service.HazMaterialListService;
import com.hopes.bizz.basic.hazard.source.vo.HazHazardSourceExcelVO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.excel.annotation.RequestExcel;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import com.hopes.boss.common.security.annotation.Inner;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 重大危险源
 *
 * @author daijie
 * @date 2023-10-25 11:00:10
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/hazardSource")
@Tag(description = "hazardSource", name = "重大危险源管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class HazHazardSourceController {

    private final HazHazardSourceService hazHazardSourceService;

    private final HazMaterialListService hazMaterialListService;

    /**
     * 分页查询
     *
     * @param page            分页对象
     * @param hazHazardSource 重大危险源
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
//    @PreAuthorize("@pms.hasPermission('haz_hazardSource_view')")
    public R getHazHazardSourcePage(Page page, HazHazardSourceEntity hazHazardSource) {
        LambdaQueryWrapper<HazHazardSourceEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtils.isNotNull(hazHazardSource.getOrgId()), HazHazardSourceEntity::getOrgId, hazHazardSource.getOrgId());
        wrapper.eq(StringUtils.isNotBlank(hazHazardSource.getHazardSourceLevel()), HazHazardSourceEntity::getHazardSourceLevel, hazHazardSource.getHazardSourceLevel());
        wrapper.like(StringUtils.isNotBlank(hazHazardSource.getHazardName()), HazHazardSourceEntity::getHazardName, hazHazardSource.getHazardName());

        return R.ok(hazHazardSourceService.page(page, wrapper));
    }


    /**
     * 获取个数
     *
     * @return 获取个数
     */
    @Operation(summary = "获取个数", description = "获取个数")
    @GetMapping("/count")
    @Inner(value = false)
    public R getHazHazardSourceCount() {
        return R.ok(hazHazardSourceService.count());
    }

    /**
     * 查询列表
     *
     * @return 查询列表
     */
    @Operation(summary = "查询列表", description = "查询列表")
    @GetMapping("/list")
    @Inner(value = false)
    public R getHazHazardSourceList() {
        return R.ok(hazHazardSourceService.list());
    }

    /**
     * 实时监控
     *
     * @param page            分页对象
     * @param hazHazardSource 重大危险源
     * @return
     */
    @Operation(summary = "实时监控", description = "实时监控")
    @GetMapping("/om")
//    @PreAuthorize("@pms.hasPermission('haz_hazardSource_view')")
    public R getHazHazardSourceOmPage(Page page, HazHazardSourceEntity hazHazardSource) {
        return R.ok(hazHazardSourceService.getHazHazardSourceOmPage(page, hazHazardSource));
    }


    /**
     * 通过id查询重大危险源
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
//    @PreAuthorize("@pms.hasPermission('haz_hazardSource_view')")
    @Inner(value = false)
    public R getById(@PathVariable("id") Long id) {
        return R.ok(hazHazardSourceService.getById(id));
    }

    /**
     * 通过id查询实时监控详情
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询实时监控详情", description = "通过id查询实时监控详情")
    @GetMapping("/getOmById/{id}")
    @PreAuthorize("@pms.hasPermission('haz_hazardSource_view')")
    public R getOmById(@PathVariable("id") Long id) {
        return R.ok(hazHazardSourceService.getOmById(id));
    }

    /**
     * 新增重大危险源
     *
     * @param hazHazardSourceDTO 重大危险源
     * @return R
     */
    @Operation(summary = "新增重大危险源", description = "新增重大危险源")
    @SysLog("新增重大危险源")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('haz_hazardSource_add')")
    @Transactional(rollbackFor = Exception.class)
    public R save(@RequestBody HazHazardSourceDTO hazHazardSourceDTO) {
        HazHazardSourceEntity entity = hazHazardSourceDTO.toPO();
        boolean save = hazHazardSourceService.save(entity);

        hazMaterialListService.init(entity);
        return R.ok();
    }

    /**
     * 修改重大危险源
     *
     * @param hazHazardSourceDTO 重大危险源
     * @return R
     */
    @Operation(summary = "修改重大危险源", description = "修改重大危险源")
    @SysLog("修改重大危险源")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('haz_hazardSource_edit')")
    public R updateById(@RequestBody HazHazardSourceDTO hazHazardSourceDTO) {
        return R.ok(hazHazardSourceService.updateById(hazHazardSourceDTO.toPO()));
    }

    /**
     * 关联视频
     *
     * @param videoDTO 关联视频信息
     * @return R
     */
    @Operation(summary = "关联视频", description = "关联视频")
    @PutMapping("/video")
    @PreAuthorize("@pms.hasPermission('haz_hazardSource_edit')")
    public R updateById(@RequestBody HazHazardSourceVideoDTO videoDTO) {
        if (StringUtils.isNotBlank(videoDTO.getVideoCodes())) {
            HazHazardSourceEntity byId = hazHazardSourceService.getById(videoDTO.getId());
            byId.setVideoCodes(videoDTO.getVideoCodes());
            return R.ok(hazHazardSourceService.updateById(byId));
        }
        return R.ok();
    }

    /**
     * 通过id删除重大危险源
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除重大危险源", description = "通过id删除重大危险源")
    @SysLog("通过id删除重大危险源")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('haz_hazardSource_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(hazHazardSourceService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/template")
    @PreAuthorize("@pms.hasPermission('haz_hazardSource_export')")
    public List<HazHazardSourceExcelVO> template() {
        return hazHazardSourceService.exportExcel(Collections.singletonList(new HazHazardSourceEntity()));
    }

    /**
     * 导出excel 表格
     *
     * @param hazHazardSource 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('haz_hazardSource_export')")
    public List<HazHazardSourceExcelVO> export(HazHazardSourceEntity hazHazardSource) {
        LambdaQueryWrapper<HazHazardSourceEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StringUtils.isNotBlank(hazHazardSource.getHazardSourceLevel()), HazHazardSourceEntity::getHazardSourceLevel, hazHazardSource.getHazardSourceLevel());
        wrapper.like(StringUtils.isNotBlank(hazHazardSource.getHazardName()), HazHazardSourceEntity::getHazardName, hazHazardSource.getHazardName());
        return hazHazardSourceService.exportExcel(hazHazardSourceService.list(wrapper));
    }

    /**
     * 导入excel 表格
     *
     * @param hazHazardSourceExcelDtoList 查询条件
     * @param bindingResult               错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('haz_hazardSource_export')")
    public R importExcel(@RequestExcel List<HazHazardSourceExcelDTO> hazHazardSourceExcelDtoList, BindingResult bindingResult) {
        return hazHazardSourceService.importExcel(hazHazardSourceExcelDtoList, bindingResult);
    }
}
